package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    DtsAdminMapper adminMapper;
    @Override
    public Map<String, Object> findPage(int page, int limit, String sort, String order, String username) {

        IPage<DtsAdmin> ipage = new Page<>(page, limit);
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        if(! StringUtils.isEmpty(username)) {
            wrapper.eq("username",username);
        }
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        adminMapper.selectPage(ipage, wrapper);
        List<DtsAdmin> adminList = ipage.getRecords();
        adminList = adminList.stream().map(admin -> {
            admin.setPassword("");
            return admin;
        }).collect(Collectors.toList());
        //创建返回值对象
        Map<String, Object> result = new HashMap<>();
        result.put("total", ipage.getTotal());
        result.put("items", adminList );

        return result;
    }
}
